//
//  UploadAPI.swift
//  Employee
//
//  Created by swordjoy on 2017/4/12.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import Moya
import SwiftyJSON


let endpointClosure = { (target: FileAPI) -> Endpoint<FileAPI> in
    let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
    let token = Token.shared.token
    return defaultEndpoint.adding(newHTTPHeaderFields: ["Authorization": ("bearer " + token), "User-Agent": "iOS", "Cookie": Cookie.cookie])
}

let FileProvider = MoyaProvider<FileAPI>(endpointClosure: endpointClosure,
    plugins:[
            NetworkLoggerPlugin(verbose: true)
    ]
)


public enum FileAPI {
    case uploadImages(images: [Data], thumbSquareSide: String)
    case uploadHeadIconImage(image: Data, thumbSquareSide: String)
}

extension FileAPI: TargetType {

    /// The path to be appended to `baseURL` to form the full `URL`.
    public var path: String {
    switch self {
        case .uploadImages:
            return "/upload/images.json"
        case .uploadHeadIconImage:
            return "/ms/v\(SJApp.version)/avatar.json"
        }
    }
  
  
    /// The parameters to be incoded in the request.
    public var parameters: [String : Any]? {
        switch self {
            case .uploadImages:
                return nil
            case .uploadHeadIconImage:
                return nil
        }
    }
  
    /// The target's base `URL`.
    public var baseURL: URL {
        return URL(string: GlobalConfig.BASE_OUT_URL)!
    }

    /// The HTTP method used in the request.
    public var method: Moya.Method {
        return .post
    }
  
    public var task: Task {
        switch self {
            case let .uploadImages(imageDatas, thumbSquareSide):
            var mutDatas = [MultipartFormData]()
            for (index, value) in imageDatas.enumerated() {
                mutDatas.append(MultipartFormData(provider: .data(value), name: "images", fileName: "files\(index).jpeg", mimeType: "image/jpeg"))
            }
            if let sideData = thumbSquareSide.sj.dataValue {
                mutDatas.append(MultipartFormData(provider: .data(sideData), name: "width"))
                mutDatas.append(MultipartFormData(provider: .data(sideData), name: "height"))
            }
            return .upload(.multipart(mutDatas))
        case let .uploadHeadIconImage(imageData, thumbSquareSide):
            var mutDatas = [MultipartFormData]()
            mutDatas.append(MultipartFormData(provider: .data(imageData), name: "file", fileName: "file\(index).jpeg", mimeType: "image/jpeg"))
            if let sideData = thumbSquareSide.sj.dataValue {
                mutDatas.append(MultipartFormData(provider: .data(sideData), name: "width"))
                mutDatas.append(MultipartFormData(provider: .data(sideData), name: "height"))
            }
            return .upload(.multipart(mutDatas))
        }
    }
    
    
  
    /// The method used for parameter encoding.
    public var parameterEncoding: ParameterEncoding {
        return URLEncoding.default
    }

    // 测试使用,暂时用不上
    public var sampleData: Data {
        return Data()
    }
  
}
